package com.gymmsystem.GYMMSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gymmsystem.GYMMSystem.entity.MemberCardType;
import com.gymmsystem.GYMMSystem.mapper.MemberCardTypeMapper;
import com.gymmsystem.GYMMSystem.service.MemberCardTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * 会员卡类型服务实现类
 */
@Slf4j
@Service
public class MemberCardTypeServiceImpl extends ServiceImpl<MemberCardTypeMapper, MemberCardType>
        implements MemberCardTypeService {

    /**
     * 状态常量
     */
    private static class Status {
        public static final int DISABLED = 0; // 下架
        public static final int ENABLED = 1;  // 上架
    }

    /**
     * 获取所有可用的会员卡类型
     */
    @Override
    public List<MemberCardType> getAvailableCardTypes() {
        QueryWrapper<MemberCardType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", Status.ENABLED)
                .orderByAsc("price");
        return this.list(queryWrapper);
    }

    /**
     * 根据卡类型名称查询
     */
    @Override
    public MemberCardType getByCardName(String cardName) {
        QueryWrapper<MemberCardType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("card_name", cardName);
        return this.getOne(queryWrapper);
    }

    /**
     * 更新会员卡价格
     */
    @Override
    @Transactional
    public boolean updateCardPrice(Long cardTypeId, BigDecimal price) {
        if (price == null || price.compareTo(BigDecimal.ZERO) < 0) {
            throw new IllegalArgumentException("价格不能为负数");
        }

        MemberCardType cardType = this.getById(cardTypeId);
        if (cardType == null) {
            throw new RuntimeException("会员卡类型不存在");
        }

        cardType.setPrice(price);
        return this.updateById(cardType);
    }

    /**
     * 更新会员卡状态
     */
    @Override
    @Transactional
    public boolean updateCardStatus(Long cardTypeId, Integer status) {
        if (status != 0 && status != 1) {
            throw new IllegalArgumentException("状态值不合法");
        }

        MemberCardType cardType = this.getById(cardTypeId);
        if (cardType == null) {
            throw new RuntimeException("会员卡类型不存在");
        }

        cardType.setStatus(status);
        return this.updateById(cardType);
    }

    /**
     * 更新会员卡价格和状态
     */
    @Override
    @Transactional
    public boolean updateCardPriceAndStatus(Long cardTypeId, BigDecimal price, Integer status) {
        if (price == null || price.compareTo(BigDecimal.ZERO) < 0) {
            throw new IllegalArgumentException("价格不能为负数");
        }

        if (status != 0 && status != 1) {
            throw new IllegalArgumentException("状态值不合法");
        }

        return baseMapper.updatePriceAndStatus(cardTypeId, price, status) > 0;
    }

    /**
     * 初始化默认会员卡类型
     */
    @Override
    @Transactional
    public void initDefaultCardTypes() {
        if (this.count() > 0) {
            log.info("会员卡类型已存在，跳过初始化");
            return;
        }

        // 创建默认会员卡类型
        List<MemberCardType> defaultCards = Arrays.asList(
                createCardType("月卡", new BigDecimal("299.00")),
                createCardType("季卡", new BigDecimal("799.00")),
                createCardType("年卡", new BigDecimal("2599.00"))
        );

        this.saveBatch(defaultCards);
        log.info("默认会员卡类型初始化完成");
    }

    private MemberCardType createCardType(String name, BigDecimal price) {
        MemberCardType card = new MemberCardType();
        card.setCardName(name);
        card.setPrice(price);
        card.setStatus(MemberCardType.Status.ENABLED);
        card.setStatus(Status.ENABLED);
        return card;
    }
}